import logging
import os

class Logger():

    def __init__(self):
        # 记录器
        self.logger = logging.getLogger()
        self.logger.setLevel(logging.DEBUG)

        # 处理器
        log_path = os.path.abspath('log')
        if os.path.lexists(log_path) == False:
            os.makedirs(log_path)
        FH = logging.FileHandler(os.path.join(log_path, 'my_log.log'), encoding='utf-8')
        FH.setLevel(logging.WARNING)
        CH = logging.StreamHandler()
        CH.setLevel(logging.INFO)

        # 日志格式
        formatter = logging.Formatter("%(asctime)s|%(levelname)s|%(filename)s|%(lineno)s|%(message)s")

        # 处理器设置输出格式
        FH.setFormatter(formatter)
        CH.setFormatter(formatter)

        # 处理器添加到记录器中
        self.logger.addHandler(FH)
        self.logger.addHandler(CH)

logger = Logger().logger